import grails.plugin.springsecurity.SpringSecurityService
import com.walkingfiles.registry.Area
import com.walkingfiles.registry.User;
import com.walkingfiles.registry.UserArea;

class CurrentAreaFilters {
	def springSecurityService


	    def filters = {
	    notAreaSelect(controller: 'area', action: 'select', invert: true) {
        before = {
			if (springSecurityService.principal instanceof grails.plugin.springsecurity.userdetails.GrailsUser) {
				def userName = springSecurityService.principal.username
                def user = User.findByUsername(userName)
				def userArea = UserArea.findByUser(user)
				def area = userArea.area
				
				session.currentAreaName = area.name
				session.currentAreaId = area.id	
//				if (springSecurityService.principal.username == 'Mara') {
//					session.currentAreaId = 1
//                  session.currentAreaName = Area.get(1).name
//				}
//				else {
//					session.currentAreaId = 2
//                    session.currentAreaName = Area.get(2).name
//				}
			}
		}
	  }
   }
}
